package com.shujia.day13;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Arrays;

public class Code12FileTest {

    public static void main(String[] args) {
        /*
            public String[] list(FilenameFilter filter)
            public File[] listFiles(FilenameFilter filter)
         */

        File data = new File("data");
        String[] lists = data.list(
                new FilenameFilter() { //接口的多态
                    @Override
                    public boolean accept(File dir, String name) {  // 该重写的方法在list方法内部调用了
                        // 传入了当前对象，及其子文件或目录
                        File file = new File(dir, name);
                        return file.isFile() && file.getName().endsWith(".txt");
                    }
                });
        System.out.println(Arrays.toString(lists));

//        data.listFiles();
        /*
            源码:
                public String[] list(FilenameFilter filter) {
                String names[] = list();
                if ((names == null) || (filter == null)) {
                    return names;
                }
                List<String> v = new ArrayList<>();
                for (int i = 0 ; i < names.length ; i++) {
                    if (filter.accept(this, names[i])) {  // 通过自定义的accept函数判断那些name需要添加到List中
                        v.add(names[i]);
                    }
                }
                return v.toArray(new String[v.size()]);
            }
         */

    }
}
